<template>
  <div class="tab-bar">
    <van-tabbar v-model="active" ref="bar" route safe-area-inset-bottom>
      <van-tabbar-item name="home" icon="home-o" replace to="/"
      >首页
      </van-tabbar-item>
      <van-tabbar-item name="search" icon="search" to="/category"
      >分类
      </van-tabbar-item>
      <van-tabbar-item name="friends" icon="shopping-cart-o" to="/trollery"
      >购物车
      </van-tabbar-item>
      <van-tabbar-item name="setting" icon="friends-o" :to="to_url">
        {{ is_login }}
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script type="text/ecmascript-6">
import { NEED_LOGIN, MY_SELF } from '@/router/router';

export default {
  name: "TabBar",
  components: {},
  props: {
    isPadding: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      active: 0,
      token: localStorage.getItem('token')
    };
  },
  methods: {
    windowResize() {
      let bar_height =
        this.$refs.bar.$el.getBoundingClientRect().height;
      this.$el.previousElementSibling.style.paddingBottom = bar_height + 'px';
    }
  },
  computed: {
    is_login() {
      return this.token ? '个人中心' : '未登录';
    },
    to_url() {
      return this.token ? MY_SELF : NEED_LOGIN;
    }
  },
  mounted() {
    if (this.isPadding) {
      this.windowResize();
      window.addEventListener("resize", this.windowResize);
    }
  },
  destroyed() {
    if (this.isPadding) {
      window.removeEventListener("resize", this.windowResize);
    }
  }
};
</script>

<style scoped lang="stylus">
  .tab-bar
    z-index 99
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;

    .van-tabbar--fixed
      position static

    .van-tabbar
      box-shadow 0 0 10px 0 hsla(0, 6%, 58%, 0.6)
</style>
